﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HPortal.Lib;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.IO;

namespace HPortal.Backend
{
    public partial class DSDonvi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!IsPostBack)
                {
                    grvManager_DataBind();
                }
            }
        }
        private void grvManager_DataBind()
        {
            DataTable la = DatabaseHelper.Select("Exec [KH_TimkiemDonvi] N" + DatabaseHelper.EscapeQuote(txtKeyword.Text));
            grvChunhiem.DataSource = la;
            grvChunhiem.DataBind();
        }

        protected void lnkChonTiendo_Click(object sender, EventArgs e)
        {
            string DonviID = ((LinkButton)sender).CommandArgument;
            DataTable t = DatabaseHelper.Select("Select * From KH_Donvi Where DonviID=" + DonviID);
            txtDonvi.Text = t.Rows[0]["Ten"].ToString();
            txtDiachi.Text = t.Rows[0]["Diachi"].ToString();
            txtDienthoai.Text = t.Rows[0]["Dienthoai"].ToString();
            txtEMail.Text = t.Rows[0]["EMail"].ToString();
            cmdEditTiendo.Enabled = true;
            cmdEditTiendo.CommandArgument = DonviID;
        }

        protected void lnkXoaTiendo_Click(object sender, EventArgs e)
        {
            string DonviID = ((LinkButton)sender).CommandArgument;
            DatabaseHelper.Execute("Delete From KH_Donvi Where DonviID=" + DonviID);
            txtDonvi.Text = "";
            txtDiachi.Text = "";
            txtDienthoai.Text = "";
            txtEMail.Text = "";
            txtKeyword.Text = "";

            grvManager_DataBind();
        }

        protected void cmdEditTiendo_Click(object sender, EventArgs e)
        {
            string DonviID = ((Button)sender).CommandArgument;
            DatabaseHelper.Select("Update KH_Donvi Set Ten=" + DatabaseHelper.EscapeQuoteUnicode(txtDonvi.Text) + ",Diachi=" + DatabaseHelper.EscapeQuoteUnicode(txtDiachi.Text) + ",Dienthoai=" + DatabaseHelper.EscapeQuoteUnicode(txtDienthoai.Text) + ",Email=" + DatabaseHelper.EscapeQuoteUnicode(txtEMail.Text) + " Where DonviID=" + DonviID);

            grvManager_DataBind();
        }

        protected void cmdSearch_Click(object sender, EventArgs e)
        {
            grvManager_DataBind();
        }

        protected void cmdThemTiendo_Click(object sender, EventArgs e)
        {
            DatabaseHelper.Select("Exec [KH_InsertDonvi] " + DatabaseHelper.EscapeQuoteUnicode(txtDonvi.Text) + "," + DatabaseHelper.EscapeQuoteUnicode(txtDiachi.Text) + "," + DatabaseHelper.EscapeQuoteUnicode(txtDienthoai.Text) + "," + DatabaseHelper.EscapeQuoteUnicode(txtEMail.Text));
            grvManager_DataBind();
        }

        protected void grvChunhiem_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvChunhiem.PageIndex = e.NewPageIndex;
            grvManager_DataBind();
        }
    }
}